#!/usr/bin/env sh

GSH_ROOT=$(cd "$(dirname "$0")/.." && pwd -P)
GSH_MISSIONS=$GSH_ROOT/missions
GSH_CONFIG=$GSH_ROOT/.config

if [ "$1" = "-r" ]
then
  RELATIVE=1
  shift
fi

# get the mission directory
n=$1
dir=$(awk -v n="$n" -v DIR="$GSH_MISSIONS" '/^\s*[#!]/{next} /^$/{next} {N++} (N == n){print DIR "/" $0; exit}' "$GSH_CONFIG/index.idx")
if [ -z "$dir" ]
then
  echo "Error: missiondir, mission '$n' not found." >&2
  exit 1
fi
dir=$(readlink-f "$dir")

if [ -z "$RELATIVE" ]
then
  echo "$dir"
else
  echo "${dir#$GSH_MISSIONS/}"
fi


